from django.contrib.auth import authenticate
from django.core.exceptions import ValidationError
from django.core.validators import RegexValidator
from django.shortcuts import render,redirect,HttpResponse
from app02.utils.bootrstap import BootrsapModelForm
from django import forms
from app02 import models
from django.contrib import messages


def index_list(request):
    if request.method=='GET':
        return render(request, 'index.html')

#个人资料
class StudentModelForm(BootrsapModelForm):
    class Meta:
        # 关联表
        model = models.StudentInfo
        exclude = ['pwd','h_type']
        # 判断用户输入的值
    # def clean_Studentmodlie(self):
    #     # 获取用户输入的值
    #     Smodlie = self.cleaned_data['Studentmodlie']
    #     exits=models.StudentInfo.objects.filter(Studentmodlie=Smodlie).exists()
    #     # 判断输入是否符合
    #     if exits != Smodlie:
    #         # 验证不通过
    #         raise ValidationError('手机号已存在')
    #         # 验证通过 将用户输入的值返回
    #     return Smodlie
class TeacherModelForm(BootrsapModelForm):
    class Meta:
        # 关联表
        model = models.TeachInfo
        exclude = ['pwd','h_type']
class AdminModelForm(BootrsapModelForm):
    class Meta:
        # 关联表
        model = models.AdminInfo
        exclude = ['pwd','h_type']



def index_profile(request):
   info_data = request.session.get('info')
   users = info_data.get('user')
   if request.method=='GET':
       # print(info_data)
       if info_data.get('h_type') == '0':
           Teacher_row = models.TeachInfo.objects.filter(user=users).first()
           tokens = TeacherModelForm(instance=Teacher_row)
           return render(request,'teacher_profile.html',{'tokens':tokens})
       #     # 判断用户类型，根据用户类型返回用户对应视图
       elif info_data.get('h_type') == '1':
           # 根据用户名获取对应的行
           row_s = models.StudentInfo.objects.filter(user=users).first()
           tokens = StudentModelForm(instance=row_s)
           return render(request, 'student_profile.html',{'tokens':tokens})
       elif info_data.get('h_type') == '2':
           row_s = models.AdminInfo.objects.filter(user=users).first()
           tokens =AdminModelForm(instance=row_s)
           return render(request, 'admin_profile.html',{'tokens':tokens})
   else:
       #获取到用户发生过来的post请求
       if info_data.get('h_type') == '0':
           # 获取用户提交数据
           row_s = models.TeachInfo.objects.filter(user=users).first()
           tokens = TeacherModelForm(data=request.POST, instance=row_s)
           # 验证表单
           if  tokens.is_valid():
               mobile = tokens.cleaned_data.get('modlie')
               if len(mobile) != 11:
                   tokens.add_error('modlie', '手机号码格式不一致！')
                   return render(request, 'teacher_profile.html', {'tokens': tokens})
               # 自动保存到数据库
               tokens.save()
               messages.error(request, '修改成功')
               tokens.save()
               return redirect("/teacher/list/")
           # 验证失败，返回编辑页面
           return render(request, 'teacher_profile.html',{'tokens':tokens})
       #     # 判断用户类型，根据用户类型返回用户对应视图
       elif info_data.get('h_type') == '1':
           # 获取用户提交数据
           row_s = models.StudentInfo.objects.filter(user=users).first()
           tokens=StudentModelForm(data=request.POST,instance=row_s)
           #验证表单

           if tokens.is_valid():
               # print(tokens.cleaned_data)
               mobile=tokens.cleaned_data.get('Studentmodlie')
               if len(mobile) != 11:
                   tokens.add_error('Studentmodlie', '手机号码格式不一致！')
                   return render(request, 'student_profile.html', {'tokens': tokens})
               # 自动保存到数据库
               tokens.save()
               messages.error(request, '修改成功')
               return redirect("/student/list/")
           #验证失败，返回编辑页面
           return render(request,'student_profile.html',{'tokens':tokens})
       elif info_data.get('h_type') == '2':
           row_s = models.AdminInfoo.objects.filter(user=users).first()
           tokens= AdminModelForm(data=request.POST, instance=row_s)
           # 验证表单
           if tokens.is_valid():
               # 自动保存到数据库
               tokens.save()
               return redirect("/admins/list/")
           # 验证失败，返回编辑页面
           return render(request, 'admin_profile.html',{'tokens':tokens})
